home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / editor / EdAEJSEAttributes.js < prev    next >
Encoding:
JavaScript  |  2001-06-12  |  6.4 KB  |  218 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  */
  23.  
  24. function BuildJSEAttributeNameList()
  25. {
  26.   ClearMenulist(dialog.AddJSEAttributeNameList);
  27.   
  28.   // Get events specific to current element
  29.   var attNames = gJSAttr[gElement.localName.toLowerCase()];
  30.   var i;
  31.   var popup;
  32.   var sep;
  33.  
  34.   if (attNames && attNames.length)
  35.   {
  36.     for (i = 0; i < attNames.length; i++)
  37.       AppendStringToMenulist(dialog.AddJSEAttributeNameList, attNames[i]);
  38.  
  39.     popup = dialog.AddJSEAttributeNameList.firstChild;
  40.     if (popup)
  41.     {
  42.       sep = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuseparator");
  43.       if (sep)
  44.         popup.appendChild(sep);
  45.     }        
  46.   }
  47.  
  48.   // Always add core JS events
  49.   for (i = 0; i < gCoreJSEvents.length; i++)
  50.   {
  51.     if (gCoreJSEvents[i] == "-")
  52.     {
  53.       if (!popup)
  54.         popup = dialog.AddJSEAttributeNameList.firstChild;
  55.  
  56.       sep = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuseparator");
  57.  
  58.       if (popup && sep)
  59.         popup.appendChild(sep);
  60.     }
  61.     else
  62.       AppendStringToMenulist(dialog.AddJSEAttributeNameList, gCoreJSEvents[i]);
  63.   }
  64.   
  65.   dialog.AddJSEAttributeNameList.selectedIndex = 0;
  66.  
  67.   // Use current name and value of first tree item if it exists
  68.   onSelectJSETreeItem();
  69.  
  70.   dialog.AddJSEAttributeNameList.focus();
  71. }
  72.  
  73. // build attribute list in tree form from element attributes
  74. function BuildJSEAttributeTable()
  75. {
  76.   var nodeMap = gElement.attributes;
  77.   if (nodeMap.length > 0)
  78.   {
  79.     var added = false;
  80.     for (var i = 0; i < nodeMap.length; i++)
  81.     {
  82.       if( CheckAttributeNameSimilarity( nodeMap[i].nodeName, JSEAttrs ) )
  83.         continue;   // repeated or non-JS handler, ignore this one and go to next
  84.       if( !IsEventHandler( nodeMap[i].nodeName ) )
  85.         continue; // attribute isn't an event handler.
  86.       var name  = nodeMap[i].nodeName.toLowerCase();
  87.       var value = gElement.getAttribute(nodeMap[i].nodeName);
  88.       if (AddTreeItem( name, value, "JSEAList", JSEAttrs )) // add item to tree
  89.         added = true;
  90.     }
  91.  
  92.     // Select first item
  93.     if (added)
  94.       dialog.AddJSEAttributeTree.selectedIndex = 0;
  95.   }
  96. }
  97.  
  98. // check to see if given string is an event handler.
  99. function IsEventHandler( which )
  100. {
  101.   var handlerName = which.toLowerCase();
  102.   var firstTwo = handlerName.substring(0,2);
  103.   if (firstTwo == "on")
  104.     return true;
  105.   else
  106.     return false;
  107. }
  108.  
  109. function onSelectJSEAttribute()
  110. {
  111.   if(!gDoOnSelectTree)
  112.     return;
  113.  
  114.   dialog.AddJSEAttributeValueInput.value = 
  115.       GetAndSelectExistingAttributeValue(dialog.AddJSEAttributeNameList.label, "JSEAList");
  116. }
  117.  
  118. function onSelectJSETreeItem()
  119. {
  120.   var tree = dialog.AddJSEAttributeTree;
  121.   if (tree && tree.selectedItems && tree.selectedItems.length)
  122.   {
  123.     var name = GetTreeItemAttributeStr(tree.selectedItems[0]);
  124.  
  125.     // Select attribute name in list
  126.     if (dialog.AddJSEAttributeNameList.firstChild)
  127.     {
  128.       var arr = dialog.AddJSEAttributeNameList.firstChild.getElementsByAttribute('label', name);
  129.       if (arr && arr.length)
  130.         dialog.AddJSEAttributeNameList.selectedItem = arr[0];
  131.  
  132.       // Set value input to that in tree (no need to update this in the tree)
  133.       gUpdateTreeValue = false;
  134.       dialog.AddJSEAttributeValueInput.value =  GetTreeItemValueStr(tree.selectedItems[0]);
  135.       gUpdateTreeValue = true;
  136.     }
  137.   }
  138. }
  139.  
  140. function onInputJSEAttributeValue()
  141. {
  142.   if (gUpdateTreeValue)
  143.   {
  144.  
  145.     var name = TrimString(dialog.AddJSEAttributeNameList.label);
  146.     var value = TrimString(dialog.AddJSEAttributeValueInput.value);
  147.  
  148.     // Update value in the tree list
  149.     // Since we have a non-editable menulist, 
  150.     //   we MUST automatically add the event attribute if it doesn't exist
  151.     if (!UpdateExistingAttribute( name, value, "JSEAList" ))
  152.       AddTreeItem( name, value, "JSEAList", JSEAttrs );
  153.   }
  154. }
  155.  
  156. function editJSEAttributeValue(targetCell)
  157. {
  158.   if (IsNotTreeHeader(targetCell))
  159.     dialog.AddJSEAttributeValueInput.inputField.select();
  160. }
  161.  
  162. function UpdateJSEAttributes()
  163. {
  164.   var JSEAList = document.getElementById("JSEAList");
  165.   var i;
  166.  
  167.   // remove removed attributes
  168.   for (i = 0; i < JSERAttrs.length; i++)
  169.   {
  170.     name = JSERAttrs[i];
  171.     if (gElement.getAttribute(name))
  172.       gElement.removeAttribute(name);
  173.   }
  174.  
  175.   // Add events
  176.   for (i = 0; i < JSEAList.childNodes.length; i++)
  177.   {
  178.     var item = JSEAList.childNodes[i];
  179.  
  180.     // set the event handler
  181.     gElement.setAttribute( GetTreeItemAttributeStr(item), GetTreeItemValueStr(item) );
  182.   }
  183. }
  184.  
  185. function RemoveJSEAttribute()
  186. {
  187.   var treechildren = dialog.AddJSEAttributeTree.lastChild;
  188.  
  189.   // This differs from HTML and CSS panels: 
  190.   // We reselect after removing, because there is not
  191.   //  editable attribute name input, so we can't clear that
  192.   //  like we do in other panels
  193.   var newIndex = dialog.AddJSEAttributeTree.selectedIndex;
  194.  
  195.   // We only allow 1 selected item
  196.   if (dialog.AddJSEAttributeTree.selectedItems.length)
  197.   {
  198.     var item = dialog.AddJSEAttributeTree.selectedItems[0];
  199.  
  200.     // Name is the text of the treecell
  201.     var attr = GetTreeItemAttributeStr(item);
  202.  
  203.     // remove the item from the attribute array
  204.     if (newIndex >= (JSEAttrs.length-1))
  205.       newIndex--;
  206.  
  207.     // remove the item from the attribute array
  208.     JSERAttrs[JSERAttrs.length] = attr;
  209.     RemoveNameFromAttArray(attr, JSEAttrs);
  210.  
  211.     // Remove the item from the tree
  212.     treechildren.removeChild (item);
  213.  
  214.     // Reselect an item
  215.     dialog.AddJSEAttributeTree.selectedIndex = newIndex;
  216.   }
  217. }
  218.